import { FormSchema } from '@/components/Form';
import { useDictStore } from '@/store/modules/dict';
import { renderOptionTag, renderPopoverMemberSumma } from '@/utils';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { cloneDeep } from 'lodash-es';
import { NTag } from 'naive-ui';
import { h, ref } from 'vue';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public orderUid = ''; // 订单uid
  public storeId = 0; // 商家id
  public productId = 0; // 产品id
  public userId = 0; // 购买者id
  public recipient = ''; // 收件人
  public shippingProvinceId = 0; // 省市区id
  public shippingAddress = ''; // 详细地址
  public tel = ''; // 联系方式
  public status = 1; // 状态 
  public expressCompany = null; // 快递公司
  public expressNumber = ''; // 快递单号
  public operatorId = 0; // 操作发货人
  public createdAt = ''; // 创建时间
  public productMainImage = [];
  public dispatchTime = ''; // 发货时间
  public updatedAt = ''; // 更新时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  recipient: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入收件人',
  },
  shippingProvinceId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入省市区id',
  },
  shippingAddress: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入详细地址',
  },
  tel: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入联系方式',
  },
  status: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入状态 ',
  },
  expressCompany: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入快递公司',
  },
  expressNumber: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入快递单号',
  },
  operatorId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入操作发货人',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  // {
  //   field: 'id',
  //   component: 'NInputNumber',
  //   label: 'id',
  //   componentProps: {
  //     placeholder: '请输入id',
  //     onUpdateValue: (e: any) => {
  //       console.log(e);
  //     },
  //   },
  // },
  {
    field: 'orderUid',
    component: 'NInput',
    label: '订单uid',
    componentProps: {
      placeholder: '请输入订单uid',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'userId',
    component: 'NSelect',
    label: '购买者',
    componentProps: {
      placeholder: '请输入购买者',
      options: dict.getOption('memberOption'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '状态 ',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择状态 ',
      options: dict.getOption('ExpressStatusOptions'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'expressNumber',
    component: 'NInput',
    label: '快递单号',
    componentProps: {
      placeholder: '请输入快递单号',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '创建时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: '序号',
    key: 'id',
    align: 'left',
    width: 80,
  },
  {
    title: '订单uid',
    key: 'orderUid',
    align: 'left',
    width: 200,
  },
  {
    title: '商品名称',
    key: 'productName',
    align: 'left',
    width: -1,
  },
  {
    title: '商品主图',
    key: 'productMainImage',
    align: 'left',
    width: -1,
    render(row: State) {
      return h('img', {
        src: row.productMainImage[0],
        alt: '图片',
        style: {
          width: '50px',
          height: '50px',
        },
      });
    },
  },
  
  // {
  //   title: '省市区',
  //   key: 'shippingProvinceId',
  //   align: 'left',
  //   width: -1,
  //   render(row: State) {
  //    return h(CityLabel, { provinceId: row.shippingProvinceId })
  //   },
  // },
  // {
  //   title: '详细地址',
  //   key: 'shippingAddress',
  //   align: 'left',
  //   width: -1,
  // },
  
  {
    title: '状态 ',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('ExpressStatusOptions', row.status);
    },
  },
  {
    title: '快递公司',
    key: 'expressCompany',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('ExpressOptions', row.expressCompany);
    },
  },
  {
    title: '快递单号',
    key: 'expressNumber',
    align: 'left',
    width: 150,
    render(row: State) {
      if (!row.expressNumber) {
        return h(NTag, { type: 'error' }, () => '未发货');
      }
      return row.expressNumber;
    }
  },
  {
    title: '收件人',
    key: 'recipient',
    align: 'left',
    width: -1,
  },
  {
    title: '联系方式',
    key: 'tel',
    align: 'left',
    width: 120,
  },
  {
    title: '操作发货人',
    key: 'adminMemberRealName',
    align: 'left',
    width: 120,
    render(row: State) {
      if (!row.operatorId) {
        return h(NTag, { type: 'error' }, () => '未发货');
      }
      return renderPopoverMemberSumma({id: row.operatorId, realName: row.adminMemberRealName, username: row.adminMemberUsername, avatar:row.adminMemberAvatar});
    },
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: 160,
  },
  {
    title: '发货时间',
    key: 'dispatchTime',
    align: 'left',
    width: 160,
  },
  {
    title: '更新时间',
    key: 'updatedAt',
    align: 'left',
    width: 160,
  },
  // {
  //   title: '门店名称',
  //   key: 'storeName',
  //   align: 'left',
  //   width: -1,
  // },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['ExpressStatusOptions', 'ExpressOptions', 'memberOption']);
}
